#include "AnimationManager.h"
#include <exception>
#ifdef _WINDOWS
	#include <Windows.h>
#endif

AnimationManager::AnimationManager(void)
{
	_currentAnimation = NULL;
}
AnimationManager::~AnimationManager(void)
{
	cleanup();
}

void AnimationManager::update()
{
	if (_currentAnimation == NULL)
	{
		#ifdef _WINDOWS
			MessageBox(NULL, "AnimationManager _currentAnimation set to NULL when updating.", "ERROR", MB_ICONSTOP);
		#endif
			throw;
	}

	if (_currentAnimation != NULL)
		_currentAnimation->update();
}

void AnimationManager::setAnimation(const std::string& id)
{
	if (_currentAnimation != NULL)
	{
		if (_currentAnimation->getID() == id)
			return;
	}

	for (auto it = _animations.begin() ; it != _animations.end(); it++)
	{
		if ((*it)->getID() == id)
		{
			if (_currentAnimation != NULL)
				deactivate();
			_currentAnimation = (*it);
			_currentAnimation->activate();
			break;
		}
	}
}


void AnimationManager::deactivate()
{
	_currentAnimation->reset();
}

int AnimationManager::getCurrentFrame()
{
	if (_currentAnimation != NULL)
	{
		return _currentAnimation->getCurrentFrame();
	}
	return 0;
}

int AnimationManager::getCurrentRow()
{
	if (_currentAnimation != NULL)
	{
		return _currentAnimation->getRow();
	}
	return 0;
}

const std::string& AnimationManager::getCurrentID()
{
	return _currentAnimation->getID();
}

void AnimationManager::setDone(bool value)
{
	_currentAnimation->_done = value;
}

bool AnimationManager::isDone()
{
	return _currentAnimation->done();
}

void AnimationManager::cleanup()
{
	for (unsigned int i = 0; i < _animations.size(); i++)
	{
		delete _animations[i];
		_animations[i] = NULL;
	}
	_animations.clear();
}

void AnimationManager::addAnimation(Animation* animation)
{
	_animations.push_back(animation);
}

void AnimationManager::setPause(bool value)
{
	_currentAnimation->_pause = value;
}

void AnimationManager::setReverse(bool value)
{
	_currentAnimation->_reverse = value;
}

void AnimationManager::flipReverse()
{
	_currentAnimation->_reverse = !_currentAnimation->_reverse;
}

void AnimationManager::reset()
{
	if (_currentAnimation != NULL)
	{
		_currentAnimation->reset();
	}
}

bool AnimationManager::isReverse()
{
	if (_currentAnimation != NULL)
	{
		return _currentAnimation->_reverse;
	}
	return false;
}